/*******************************************************************************
 * Copyright (c) 2004, 2005 Jean-Michel Lemieux, Jeff McAffer and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Hyperbola is an RCP application developed for the book
 *     Eclipse Rich Client Platform -
 *         Designing, Coding, and Packaging Java Applications
 *         (http://eclipsercp.org)
 * Contributors:
 *     Jean-Michel Lemieux and Jeff McAffer - initial implementation
 *******************************************************************************/
package org.reliablesource.prism.product.workbench;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.part.ViewPart;
import org.reliablesource.prism.ui.workbench.views.ImageViewPart;

/**
 * Configures the main Hyperbola window.
 */
public class PrismWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
		implements IPropertyListener {

	private Control page, statusline, toolbar;

	private PrismActionBarAdvisor actionBars;

	public PrismWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public void createWindowContents(Shell shell) {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		Menu menu = configurer.createMenuBar();
		shell.setMenuBar(menu);
		shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLUE));
		FormLayout layout = new FormLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		shell.setLayout(layout);
		toolbar = configurer.createCoolBarControl(shell);
		page = configurer.createPageComposite(shell);
		statusline = configurer.createStatusLineControl(shell);
		layoutNormal();
	}

	private void layoutNormal() {
		// TOOLBAR
		FormData data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		toolbar.setLayoutData(data);
		// STATUS LINE
		data = new FormData();
		data.bottom = new FormAttachment(100, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		statusline.setLayoutData(data);
		// PAGE CONTENTS
		data = new FormData();
		data.top = new FormAttachment(toolbar);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		data.bottom = new FormAttachment(statusline);
		page.setLayoutData(data);
		layout();
	}

	private void layout() {
		getWindowConfigurer().getWindow().getShell().layout(true);
		if (page != null) {
			((Composite) page).layout(true);
		}
	}

	public void setShowToolbar(boolean visible) {
		if (visible) {
			if (toolbar.isVisible())
				return;
			FormData data = (FormData) page.getLayoutData();
			data.top = new FormAttachment(toolbar, 0);
			page.setLayoutData(data);
			toolbar.setVisible(true);
		} else {
			if (!toolbar.isVisible())
				return;
			FormData data = (FormData) page.getLayoutData();
			data.top = new FormAttachment(0, 0);
			page.setLayoutData(data);
			toolbar.setVisible(false);
		}
		layout();
	}

	public boolean getShowToolbar() {
		return toolbar != null && toolbar.isVisible();
	}

	public void setShowStatusline(boolean visible) {
		if (visible) {
			if (statusline.isVisible())
				return;

			FormData data = (FormData) page.getLayoutData();
			data.bottom = new FormAttachment(statusline, 0);
			page.setLayoutData(data);
			statusline.setVisible(true);
		} else {
			if (!statusline.isVisible())
				return;

			FormData data = (FormData) page.getLayoutData();
			data.bottom = new FormAttachment(100, 0);
			page.setLayoutData(data);
			statusline.setVisible(false);
		}
		layout();
	}

	public boolean getShowStatusline() {
		return statusline != null && statusline.isVisible();
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(350, 475));
		configurer.setShowProgressIndicator(false);
		configurer.getWindow().getPartService().addPartListener(
				new IPartListener() {

					public void partOpened(IWorkbenchPart part) {
						if (part instanceof ImageViewPart) {
							part
									.addPropertyListener(PrismWorkbenchWindowAdvisor.this);
							propertyChanged(part, ViewPart.PROP_TITLE);
						}
					}

					public void partDeactivated(IWorkbenchPart part) {
					}

					public void partClosed(IWorkbenchPart part) {
						if (part instanceof ImageViewPart)
							part
									.removePropertyListener(PrismWorkbenchWindowAdvisor.this);
					}

					public void partBroughtToTop(IWorkbenchPart part) {
					}

					public void partActivated(IWorkbenchPart part) {
					}
				});
	}

	public void postWindowCreate() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setShowPerspectiveBar(false);
		configurer.setShowProgressIndicator(false);
	}

	public void postWindowOpen() {
		if(actionBars != null)
			actionBars.postWindowCreate();		
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		actionBars = new PrismActionBarAdvisor(this, configurer);
		return actionBars;
	}

	public void propertyChanged(Object source, int propId) {
		if (ViewPart.PROP_TITLE == propId && source instanceof ImageViewPart) {
			getWindowConfigurer().setTitle(((ImageViewPart) source).getPartName());
		}
	}

	public Object getData(String key) {
		return getWindowConfigurer().getData(key);
	}
}
